package com.uoko.common.date;

import org.joda.time.DateTime;
import org.joda.time.Hours;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * <p>
 *
 * </p>
 *
 * @author xianguo
 * Email xianguo@uoko.com
 * created by 2018/6/6 0006
 */
public class Dates {
    /**
     * <p>
     * 获取当前的时间：字符串类型,传入具体的格式 例如："yyyy-MM-dd HH:mm:ss"
     * </p>
     *
     */
    public static String getCurrentTimeString(String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(new Date());
    }
    /**
     * <p>
     * 获取当前的时间：long类型
     * </p>
     *
     */
    public static long getCurrentTimeLong() {
        Date date = new Date();
        long currentTime = date.getTime();
        return currentTime;
    }
    /**
     * 把日期类型为pattern（例如：pattern = 'yyyy-MM-dd HH:mm:ss'）解析为long类型时间
     *
     */
    public static long parseDateMyYYMMDDToLong(String dateStr,String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern(pattern);
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (Exception ex) {

        }
        return date.getTime();
    }
    public static String formatLongToNormal(Long timespan){
        try{
            return formatDateToNormal(new Date(timespan));
        }catch(Exception e){
            return formatDateToNormal(new Date());
        }
    }

    public static String formatDateToNormal(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        date = date == null ? new Date() : date;
        return sdf.format(date);
    }
    /**
     * 把旧格式(例如：oldPattern = 'yyyyMMddHHmmss')转换为格式 (例如：newPattern = 'yyyy-MM-dd HH:mm:ss')
     *
     */
    public static String formatDateToNormal(String dateStr,String oldPattern,String newPattern) {
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern(oldPattern);
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        sdf.applyPattern(newPattern);
        return sdf.format(date);
    }
    private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static long getNowDate() {
        return System.currentTimeMillis() / 1000;
    }

    // 数据库的秒值转换为日期
    public static Date secondToDate(Integer second) {
        return new Date((long) second.intValue() * 1000);
    }

    // 将日期转为数据库的秒
    public static Integer dateToSecond(Date expireTime) {
        return (int) (expireTime.getTime() / 1000);
    }

    /**
     * 添加分钟
     *
     * @return
     */
    public static Date addMinutes(Date date, Integer minutes) {
        return new Date(date.getTime() + minutes * 60 * 1000);
    }

    /**
     * 添加天数
     *
     * @return
     */
    public static Date addDays(Date date, Integer days) {
        return new Date(date.getTime() + days * 24 * 60 * 60 * 1000);
    }

    public static String dateToString(Date date) {
        return format.format(date);
    }

    /**
     * 已当前时间为基准获取某天某小时的秒值 for example hour = 5,add = -2 表示获取前天5点整时刻的秒值
     * @param hour
     * @param add
     * @return
     */
    public static Integer getTime(int hour,int add){
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH,add);
        cal.set(Calendar.HOUR_OF_DAY,hour);
        cal.set(Calendar.MINUTE,0);
        cal.set(Calendar.SECOND,0);
        cal.set(Calendar.MILLISECOND,0);
        return (int)(cal.getTimeInMillis()/1000);
    }

    public static int daysBetween(Date d1,Date d2){
        //joda-time
        DateTime start=new DateTime(d1.getTime());
        DateTime end=new DateTime(d2.getTime());
        return Hours.hoursBetween(start,end).getHours();
    }
    /**
     * 获取一天属于星期几。星期一返回1，星期天返回7;
     *
     * @param date
     * @return
     */
    public static Integer getDayOfWeek(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return (c.get(Calendar.DAY_OF_WEEK) == 1) ? 7 : c.get(Calendar.DAY_OF_WEEK) - 1;
    }


    /**
     * 比较date1是在date2之前(只比较日期部分)。
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean beforeDate(Date date1, Date date2) {

        return daysBetween(date1, date2) < 0;
    }

    /**
     * 比较date1是在date2之后(只比较日期部分)。
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean afterDate(Date date1, Date date2) {
        return daysBetween(date1, date2) > 0;
    }

    /**
     * 比较date1和date2相等(只比较日期部分)。
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean equalDate(Date date1, Date date2) {
        // Calendar cal= Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date1).equals(format.format(date2));
    }

    /**
     * 获得date1和date2之间的间隔天数，用
     *
     * @param date1 date2
     * @return
     */
    public static int getIntervalDays(String date1, String date2) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date datetemp1;
        try {
            datetemp1 = format.parse(date1);
            Date datetemp2 = format.parse(date2);
            return (int) ((datetemp1.getTime() - datetemp2.getTime()) / (24 * 60 * 60 * 1000));
        } catch (ParseException e) {
            return -1;
        }
    }

}
